package com.empire.module.sporttery.controller.admin.footballmatchodd;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.sporttery.controller.admin.footballmatchodd.vo.*;
import com.empire.module.sporttery.dal.dataobject.footballmatchodd.FootballMatchOddDO;
import com.empire.module.sporttery.service.footballmatchodd.FootballMatchOddService;

@Tag(name = "管理后台 - 竞猜足球指数")
@RestController
@RequestMapping("/sporttery/football-match-odd")
@Validated
public class FootballMatchOddController {

    @Resource
    private FootballMatchOddService footballMatchOddService;

    @PostMapping("/create")
    @Operation(summary = "创建竞猜足球指数")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match-odd:create')")
    public CommonResult<Integer> createFootballMatchOdd(@Valid @RequestBody FootballMatchOddSaveReqVO createReqVO) {
        return success(footballMatchOddService.createFootballMatchOdd(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新竞猜足球指数")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match-odd:update')")
    public CommonResult<Boolean> updateFootballMatchOdd(@Valid @RequestBody FootballMatchOddSaveReqVO updateReqVO) {
        footballMatchOddService.updateFootballMatchOdd(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除竞猜足球指数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sporttery:football-match-odd:delete')")
    public CommonResult<Boolean> deleteFootballMatchOdd(@RequestParam("id") Integer id) {
        footballMatchOddService.deleteFootballMatchOdd(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得竞猜足球指数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match-odd:query')")
    public CommonResult<FootballMatchOddRespVO> getFootballMatchOdd(@RequestParam("id") Integer id) {
        FootballMatchOddDO footballMatchOdd = footballMatchOddService.getFootballMatchOdd(id);
        return success(BeanUtils.toBean(footballMatchOdd, FootballMatchOddRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得竞猜足球指数分页")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match-odd:query')")
    public CommonResult<PageResult<FootballMatchOddRespVO>> getFootballMatchOddPage(@Valid FootballMatchOddPageReqVO pageReqVO) {
        PageResult<FootballMatchOddDO> pageResult = footballMatchOddService.getFootballMatchOddPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FootballMatchOddRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出竞猜足球指数 Excel")
    @PreAuthorize("@ss.hasPermission('sporttery:football-match-odd:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFootballMatchOddExcel(@Valid FootballMatchOddPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FootballMatchOddDO> list = footballMatchOddService.getFootballMatchOddPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "竞猜足球指数.xls", "数据", FootballMatchOddRespVO.class,
                        BeanUtils.toBean(list, FootballMatchOddRespVO.class));
    }

}